package com.gitee.grower.util.germ

import com.germ.germplugin.api.dynamic.gui.GermGuiCheckbox
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen.ClickType
import com.gitee.grower.util.germ.germ.GermPartDSL
import taboolib.library.configuration.ConfigurationSection

class GermCheckBoxDSL : GermPartDSL<GermGuiCheckbox>() {

    var defaultPath : String? = null
    var hoverPath : String? = null
    var checkedPath : String? = null
    var hoverSound : String? = null
    var clickSound : String? = null
    var invalid = false
    var checked = false
    var width : String? = null
    var height : String? = null
    var textScale : String? = null
    var hoverTextScale : String? = null
    var texts = mutableListOf<String>()
    var hoverTexts = mutableListOf<String>()

    var onClick: (GermGuiScreen.() -> Unit)? = null

    override val type: String = "checkbox"

    override fun infuse(config: ConfigurationSection) {
        name = config.name
        config.getString("locationX")?.let { locationX = it }
        config.getString("locationY")?.let { locationY = it }
        config.getString("locationZ")?.let { locationZ = it }
        width = config.getString("width")
        height = config.getString("height")
        defaultPath = config.getString("defaultPath")
        hoverPath = config.getString("hoverPath")
        checkedPath = config.getString("checkedPath")
        textScale = config.getString("textScale")
        clickSound = config.getString("clickSound")
        hoverTextScale = config.getString("hoverTextScale")
        invalid = config.getBoolean("invalid")
        texts += config.getStringList("texts")
        hoverTexts += config.getStringList("hoverTexts")

        enable = config.getBoolean("enable")
    }


    override fun create(): GermGuiCheckbox {
        return GermGuiCheckbox(name).also {
            it.locationX = locationX
            it.locationY = locationY
            it.locationZ = locationZ
            it.enable = enable
            it.defaultPath = defaultPath
            it.hoverPath = hoverPath
            it.checkedPath = checkedPath
            it.hoverSound = hoverSound
            it.clickSound = clickSound
            it.isInvalid = invalid
            it.checked = checked
            it.textScale = textScale
            it.hoverTextScale = hoverTextScale
            it.hoverTexts = hoverTexts

            it.width = width
            it.height = height

            it.texts = texts

            onClick?.apply {
                it.registerCallbackHandler({ _, _ ->
                    onClick!!(it.germGuiScreen)
                }, ClickType.LEFT_CLICK)
            }

        }
    }


}